<template>
	<view class="container">
		<view class="user-top  u-flex u-col-center u-row-between">
			<view class="img">
				<image :src="getImg(userInfo.avatar)" mode="aspectFill" v-if="userInfo.avatar"></image>
				<image src="../../static/image/up.png" mode="aspectFill" v-else></image>
			</view>
			<view class="con">
				<view class="name" v-if="userInfo.nickname">
					{{userInfo.nickname}}
				</view>
				<button class="noname" v-else open-type="getAuthorize" scope="userInfo" onGetAuthorize="getOpenUserInfo"
					@click="getOpenUserInfo">
					授权会员信息
				</button>
				<view class="tel" v-if="userInfo.mobile">
					{{userInfo.mobile}}
				</view>
				<button class="notel" open-type="getAuthorize" scope="phoneNumber" onGetAuthorize="getPhoneNumber"
					@click="getPhoneNumber" v-else>
					授权手机号
				</button>
			</view>
		</view>
		<view class="user-list">
			<view class="item u-flex u-col-center u-row-between" @click="$pageTo('/pages/index/listOrder1')">
				<view class="l  u-flex u-col-center">
					<image src="../../static/image/u5.png" mode="aspectFill"></image>
					<text>购买订单</text>
				</view>
				<view class="r">
					<uni-icons type="right" size="16" color="#999"></uni-icons>
				</view>
			</view>
			<view class="item u-flex u-col-center u-row-between" @click="$pageTo('/pages/index/listOrder')">
				<view class="l  u-flex u-col-center">
					<image src="../../static/image/u4.png" mode="aspectFill"></image>
					<text>卖出订单</text>
				</view>
				<view class="r">
					<uni-icons type="right" size="16" color="#999"></uni-icons>
				</view>
			</view>
			<view class="item u-flex u-col-center u-row-between" @click="$pageTo('/pages/index/book')">
				<view class="l  u-flex u-col-center">
					<image src="../../static/image/u2.png" mode="aspectFill"></image>
					<text>投诉建议</text>
				</view>
				<view class="r">
					<uni-icons type="right" size="16" color="#999"></uni-icons>
				</view>
			</view>
			<view class="item u-flex u-col-center u-row-between">
				<view class="l  u-flex u-col-center">
					<image src="../../static/image/u3.png" mode="aspectFill"></image>
					<text>联系客服</text>
				</view>
				<view class="r">
					<contact-button tnt-inst-id="iA3_QVo5" scene="SCE01262333" size="24" color="#333"
						icon="/static/image/kf.png" />
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: {
					nickname: "", //昵称
					mobile: "", //手机号
					avatar: ""
				},
				canIUseAuthButton: my.canIUse('button.open-type.getAuthorize')
			}
		},
		onLoad() {
			my.setNavigationBar({
				frontColor: '#000',
				backgroundColor: '#fff',
			})
			if (!uni.getStorageSync('token')) {
				this.login()
			} else {
				this.getUserInfo()
			}
		},
		// onShow() {
		// 	if (uni.getStorageSync('token')) {
		// 		this.getUserInfo()
		// 	}
		// },
		computed: {
			getImg() {
				return (img) => {
					return img
				}
			}

		},
		methods: {
			getOpenUserInfo() {
				my.getSetting({
					withSubscriptions: true,
					success: res => {
						console.log(res.authSetting.userInfo, '2222222222222');
						if (res.authSetting.userInfo) {
							my.getOpenUserInfo({
								success: (res) => {
									let userInfo = JSON.parse(res.response).response
									this.$http.post('/api/user/saveUserInfo', {
										nickname: userInfo.nickName,
										avatar: userInfo.avatar
									}).then(res2 => {
										if (res2.code == 1) {
											this.getUserInfo()
										}
									})
								},
							});
						} else {
							my.getOpenUserInfo({
								success: (res) => {
									let userInfo = JSON.parse(res.response).response
									this.$http.post('/api/user/saveUserInfo', {
										nickname: userInfo.nickName,
										avatar: userInfo.avatar
									}).then(res2 => {
										if (res2.code == 1) {
											this.getUserInfo()
										}
									})
								},
							});
						}
					},
				});

			},
			getPhoneNumber() {
				my.getSetting({
					withSubscriptions: true,
					success: (res) => {
						console.log(res);
						if (res.authSetting.phoneNumber) {
							my.getPhoneNumber({
								success: (res) => {
									let encryptedData = res.response;
									this.$http.post('/api/user/getAliMobile', {
										content: JSON.parse(res.response).response
									}).then(res2 => {
										if (res2.code == 1) {
											this.getUserInfo()
										}
									})
								},

							});
						} else {
							// uni.showModal({
							// 	title: '温馨提示',
							// 	content: '取消绑定手机号，客服不能通过手机号联系到你提供服务！',
							// 	confirmText: "我知道了",
							// 	showCancel: false,
							// 	success: (res) => {
							// 		if (res.confirm) {
							// 			console.log('用户点击确定');
							// 		} else if (res.cancel) {
							// 			console.log('用户点击取消');
							// 		}
							// 	}
							// });
						}

					},
					fail:(err)=>{
						console.log(err);
					}
				})
			},
			login() {
				uni.login({
					provider: 'alipay',
					success: (data) => {
						console.log(data)
						let authCode = data.authCode
						console.log(authCode, 1212)
						this.$http.post('/api/user/getAliUserIdByCode', {
							code: authCode,
						}).then(res2 => {
							if (res2.code == 1) {
								uni.setStorageSync('token', res2.data.userinfo.token)
								uni.setStorageSync('userId', res2.data.userinfo.user_id)
								let loginTime = new Date().getTime()
								uni.setStorageSync('loginTime', loginTime)
								this.getUserInfo()

							} else {
								uni.showToast({
									title: res2.msg,
									icon: "none"
								});
							}
						}).catch(err2 => {
							console.log('表单错误信息：', err2);
						})
					}
				});
			},
			getUserInfo() {
				this.$http.post('/api/user/getUserInfo', {
					cate_id: this.cid,
					shop_id: this.nid,
					page: this.page,
					limit: 10
				}).then(res => {
					if (res.code == 1) {
						this.userInfo = res.data
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none"
						});
					}
				}).catch(err => {
					console.log('错误信息：', err);
				})
			},
		}
	}
</script>

<style lang="less" scoped>
	.container {
		padding: 30rpx;

		.user-list {
			margin-top: 60rpx;

			.item {
				padding: 20rpx;
				background-color: #fff;
				border-radius: 10rpx;
				margin-bottom: 20rpx;

				.l {
					image {
						width: 48rpx;
						height: 48rpx;
					}

					text {
						margin-left: 20rpx;
						color: #333;
						font-size: 30rpx;
						font-weight: 600;
					}
				}

				.r {}
			}
		}

		.user-top {
			.img {
				width: 120rpx;
				height: 120rpx;
				border-radius: 50%;
				overflow: hidden;

				image {
					width: 100%;
					height: 100%;
				}

			}

			.con {
				margin-left: 20rpx;
				flex: 1;
				width: 1rpx;

				.name {
					height: 60rpx;
					line-height: 60rpx;
					font-size: 32rpx;
					color: #333;
					font-weight: 600;
				}

				.noname {
					width: 300rpx;
					height: 60rpx;
					line-height: 60rpx;
					;
					font-size: 32rpx;
					color: #333;
					background-color: #f5f5f5;
					text-align: left;
					border: 0;
					font-weight: 600;
				}


				.tel {
					font-size: 26rpx;
					color: #666;
					height: 60rpx;
					line-height: 60rpx;
				}

				.notel {
					border: 0;
					text-align: left;
					width: 200rpx;
					height: 60rpx;
					line-height: 60rpx;
					;
					font-size: 24rpx;
					color: #333;
					background-color: #f5f5f5;
				}
			}

		}
	}
</style>